﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using CodeBuilder.DAO;
using CodeBuilder.DBM;

namespace CodeBuilder.Service
{
    public class Services
    {
        /// <summary>
        /// 查询指定表的信息
        /// </summary>
        /// <param name="tableName">要查询的表的名字</param>
        /// <returns>查询到的 dt</returns>
        public DataTable GetInfo(string tableName)
        {
            DataAccess da = new DataAccess();
            return da.GetDataTable("select * from " + tableName);
        }
        /// <summary>
        /// 得到数据库服务器中所有的数据库
        /// </summary>
        /// <returns>包含了数据库名的 dt</returns>
        public DataTable GetLibrarysName()
        {
            DataAccess da = new DataAccess();
            return da.GetDataTable("select name from sysdatabases");
        }
        /// <summary>
        /// 查询出当前链接的数据库中所有的用户表
        /// </summary>
        /// <returns>查询到的 dt</returns>
        public DataTable GetTablesName()
        {
            DataAccess da = new DataAccess();
            return da.GetDataTable("select name from sysobjects where type='u'");
        }
        /// <summary>
        /// 改变要连接的数据库
        /// </summary>
        /// <param name="server">要连接的数据库</param>
        public void ChangeServer(string server)
        {
            DataAccess da = new DataAccess();
            da.SelectServer(server);
        }
        /// <summary>
        /// 改变要连接的数据库的名字
        /// </summary>
        /// <param name="dbName">数据库名</param>
        public void ChangeDB(string dbName)
        {
            DataAccess da = new DataAccess();
            da.CreateConnectionString(dbName);
        }
        /// <summary>
        /// 利用所传参数，初始化 DBManager 类的连接字符串
        /// </summary>
        /// <param name="server">服务器名或 IP</param>
        /// <param name="uid">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="db">数据库名</param>
        public void InitConnectionInfo(string server, string uid, string pwd, string db)
        {
            DBManager.server = server;
            DBManager.uid = uid;
            DBManager.pwd = pwd;
            DBManager.database = db;
        }

        /// <summary>
        /// 得到当前连接的数据库中指定表的列信息
        /// </summary>
        /// <param name="tableName">指定的表</param>
        /// <returns>包括列信息的 DataSet</returns>
        public DataSet GetColumns(string tableName)
        {
            DataAccess da = new DataAccess();
            return da.GetDataSet("SELECT column_name,data_type,table_name,character_maximum_length,is_nullable FROM " +
                "information_schema.columns WHERE table_name = '" + tableName + "';" + "exec sp_pkeys '" +
                tableName + "'");
        }
    }
}
